import Part02.SPELDI.Person
import Part02.collectionDI.Some
import Part03.DiffAbstractBean.Teacher
import part01.ShezhiDI.Student
import com.google.gson.GsonBuilder
import org.junit.Test
import org.springframework.context.support.ClassPathXmlApplicationContext

/**
 * Created by futao on 2017/10/10.
 */
class Mytest {
    /**
     * 设值注入- 通过属性的set方法
     */
    @Test
    fun forTest() {
        val xml = ClassPathXmlApplicationContext("applicationContext.xml")
        val student = xml.getBean("student") as Student
        println(student)
    }

    /**
     * 构造注入- 通过类的带参构造方法
     */
    @Test
    fun test4gouzaoDI() {
        val xml = ClassPathXmlApplicationContext("applicationContextGouzaoDI.xml")
        val student = xml.getBean("student") as part01.gouzaoDI.Student
        println(student)
    }

    /**
     * p命名空间设值注入
     */
    @Test
    fun test4pNamespaceDI() {
        val xml = ClassPathXmlApplicationContext("applicationContextPnamespaceDI.xml")
        val student = xml.getBean("student") as part01.PnamespaceDI.Student
        //格式化Json格式
        println(GsonBuilder().setPrettyPrinting().create().toJson(student))
    }

    /**
     * 基于XML的DI-集合属性注入
     */
    @Test
    fun test4collections() {
        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextcollectionDI.xml")
        val some = classPathXmlApplicationContext.getBean("some") as Some
        println(GsonBuilder().setPrettyPrinting().create().toJson(some))
        println(some.myList.size)
    }

    /**
     * 基于XML的DI-byName方式的域属性自动注入
     */
    @Test
    fun test4autoDIbyNamefordomain() {
        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextautoDIbyNameForDomain.xml")
        val student = classPathXmlApplicationContext.getBean("student") as Part02.autoDIbyNameForDomain.Student
//        serializeNulls()->为null的值也显示出来，setPrettyPrinting()->格式化控制台输出
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student))
        println(student)
    }

    /**
     * SPEL注入
     */
    @Test
    fun test4SPEL() {
        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextSPEL.xml")
        val student = classPathXmlApplicationContext.getBean("student") as Part02.SPELDI.Student
        val person = classPathXmlApplicationContext.getBean("person") as Person
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student))
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(person))

        student.sage = 666
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student))
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(person))

        person.page = 999
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student))
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(person))

    }

    /**
     * 匿名类/匿名内部类
     */
    @Test
    fun test4NonameBean() {
        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextInnerNonameBean.xml")
        val student = classPathXmlApplicationContext.getBean("student") as Part03.InnerNonameBean.Student
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student))
    }

    /**
     * 同类抽象Bean
     */
    @Test
    fun test4SameAbstractBean() {
        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextSameAbstractBean.xml")
//        val student1 = classPathXmlApplicationContext.getBean("student1") as Part03.SameAbstractBean.Student
//        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student1))
//        val student2 = classPathXmlApplicationContext.getBean("student2") as Part03.SameAbstractBean.Student
//        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student2))
//        val student3 = classPathXmlApplicationContext.getBean("student3") as Part03.SameAbstractBean.Student
//        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student3))

        val baseStudent = classPathXmlApplicationContext.getBean("baseStudent") as Part03.SameAbstractBean.Student
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(baseStudent))
        val student11 = classPathXmlApplicationContext.getBean("student1-1") as Part03.SameAbstractBean.Student
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student11))
        val student12 = classPathXmlApplicationContext.getBean("student1-2") as Part03.SameAbstractBean.Student
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student12))


    }


    /**
     * 异类抽象Bean
     */
    @Test
    fun test4DiffAbstractBean() {
        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextDiffAbstractBean.xml")
        val student = classPathXmlApplicationContext.getBean("student") as Part03.DiffAbstractBean.Student
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student))

        val teacher = classPathXmlApplicationContext.getBean("teacher") as Teacher
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(teacher))
    }


    /**
     * 为Spring配置多个配置文件-平行关系
     */
    @Test
    fun test4ManyChildXmlFile() {
//        val arrayOf = arrayOf("applicationContextManyChildXmlFile1.xml", "applicationContextManyChildXmlFile2.xml")
        var a = arrayOf("applicationContextManyChildXmlFile1.xml", "applicationContextManyChildXmlFile2.xml")
        //使用通配符*
//        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextManyChildXmlFile*")
        val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextManyChildXmlFile1.xml", "applicationContextManyChildXmlFile2.xml")
        val student = classPathXmlApplicationContext.getBean("student") as Part03.ManyChildXmlFileParallel.Student
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student))

        val teacher = classPathXmlApplicationContext.getBean("teacher") as Part03.ManyChildXmlFileParallel.Teacher
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(teacher))

    }
}